<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/6
 * Time: 9:27
 */

namespace app\api\controller\config;
use app\api\controller\HomeCommon;
use app\api\model\Agreement;
use app\api\model\Shop;
use app\api\model\ShopStatistics;
use app\lib\exception\ParameterException;
use think\Controller;
use think\Request;


class Config extends Controller
{
    /**
     * 获取商家数和今日营业额
     * @param Request $request
     */
    public function getConfig(Request $request){
        $config = (new \app\api\model\Config())->getConfigKey();
        //$shop_number =$config['virtual_shop_number']+(new Shop())->getCount([['status','eq',2]]);
        $shop_number =$config['virtual_shop_number'];
        $where = [
          ['day','eq',strtotime(date("Y-m-d",time()))],
          ['type','eq',1]
        ];
        $turnover =$config['virtual_turnover']+(new ShopStatistics())->where($where)->sum("number");
        $returnData = [
          'shopNumber'=>$shop_number,
          'turnover'=>$turnover,
          'is_show_config'=>$config['is_show_config']
        ];
        return writeJson(200,$returnData);
    }

    /**
     * 获取协议
     * @param Request $request
     * @return \think\response\Json
     * @throws ParameterException
     */
    public function getAgreement(Request $request){
        $post = $request->post();
        if(!isset($post['type'])){
            throw new ParameterException(['msg'=>'缺少协议类型']);
        }
        $agreement = (new Agreement())->findOne([['type','eq',$post['type']]],"title,content");
        return writeJson(200,['agreement'=>$agreement]);
    }

    /**
     * 获取客服
     * @param Request $request
     * @return \think\response\Json
     * @throws ParameterException
     */
    public function getContactNumber(Request $request){
        $contact_number = (new \app\api\model\Config())->findOne([['k','eq','contact_number']],"v");
        return writeJson(200,['contact_number'=>$contact_number]);
    }

    /**
         * 获取钱包开关,分销规则,提现说明,提现规则
         * @return \think\response\Json
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
    public function getMoneyConfig(){
        $configService = new \app\api\model\Config();
        $list = $configService->where("id in (11,12,13)")->select();
        $data = [];
        foreach ($list as $k => $v){
            if($v["id"] == 11){ $data["isOpen"]=$v["v"];}
            if($v["id"] == 12){ $data["invite_rule"]=$v["v"];}
            if($v["id"] == 13){ $data["out_rule"]=$v["v"];}
        }
        return writeJson("200",$data);
    }
}